package com.mnyc.system.service.impl;


import com.alibaba.fastjson2.JSON;
import com.mnyc.common.utils.DateUtils;
import com.mnyc.system.domain.SysFileInfo;
import com.mnyc.system.mapper.SysFileInfoMapper;
import com.mnyc.system.service.ISysFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 文件信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-10
 */
@Service
public class SysFileInfoServiceImpl implements ISysFileInfoService {
    @Autowired
    private SysFileInfoMapper sysFileInfoMapper;

    /**
     * 查询文件信息
     *
     * @param fileId 文件信息主键
     * @return 文件信息
     */
    @Override
    public SysFileInfo selectSysFileInfoByFileId(Long fileId) {
        return sysFileInfoMapper.selectSysFileInfoByFileId(fileId);
    }

    /**
     * 查询文件信息列表
     *
     * @param sysFileInfo 文件信息
     * @return 文件信息
     */
    @Override
    public List<SysFileInfo> selectSysFileInfoList(SysFileInfo sysFileInfo) {
        return sysFileInfoMapper.selectSysFileInfoList(sysFileInfo);
    }

    /**
     * 新增文件信息
     *
     * @param sysFileInfo 文件信息
     * @return 结果
     */
    @Override
    public int insertSysFileInfo(SysFileInfo sysFileInfo) {
        sysFileInfo.setCreateTime(DateUtils.getNowDate());
        return sysFileInfoMapper.insertSysFileInfo(sysFileInfo);
    }

    /**
     * 修改文件信息
     *
     * @param sysFileInfo 文件信息
     * @return 结果
     */
    @Override
    public int updateSysFileInfo(SysFileInfo sysFileInfo) {
        return sysFileInfoMapper.updateSysFileInfo(sysFileInfo);
    }

    /**
     * 批量删除文件信息
     *
     * @param fileIds 需要删除的文件信息主键
     * @return 结果
     */
    @Override
    public int deleteSysFileInfoByFileIds(Long[] fileIds) {
        return sysFileInfoMapper.deleteSysFileInfoByFileIds(fileIds);
    }


    /**
     * 删除文件信息
     *
     * @param sysFileInfo 文件信息
     * @return 结果
     */
    public int deleteSysFileInfo(SysFileInfo sysFileInfo) {
        return sysFileInfoMapper.deleteSysFileInfo(sysFileInfo);
    }

    /**
     * 更新一个附件
     *
     * @param businessId
     * @param businessType
     * @param childType
     * @param fileListStr
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveOrUpdateFiles(String businessId, String businessType, String childType, String fileListStr) {
        List<SysFileInfo> fileList = JSON.parseArray(fileListStr, SysFileInfo.class);
        saveOrUpdateFiles(businessId, businessType, childType, fileList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveOrUpdateFiles(String businessId, String businessType, String childType, List<SysFileInfo> fileList) {
        SysFileInfo tmp = new SysFileInfo();
        Assert.notNull(businessId, "业务Id不能为空");
        Assert.notNull(businessType, "业务类型不能为空");
        tmp.setBusinessId(businessId);
        tmp.setBusinessType(businessType);
        tmp.setChildType(childType);
        sysFileInfoMapper.deleteSysFileInfo(tmp);
        if (!CollectionUtils.isEmpty(fileList)) {
            for (SysFileInfo f : fileList) {
                f.setBusinessType(businessType);
                f.setChildType(childType);
                f.setBusinessId(businessId);
            }
            sysFileInfoMapper.batchInsertSysFileInfo(fileList);
        }
    }

    /**
     * 读取附件：在插入的时新开事物进行插入，读取的时候新开事物进行读取，避免事物包含读取不到数据
     *
     * @param businessType
     * @param businessId
     * @param childType
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<SysFileInfo> selectSysFilelist(String businessType, String businessId, String childType) {
        SysFileInfo sysFileInfo = new SysFileInfo();
        sysFileInfo.setBusinessType(businessType);
        sysFileInfo.setBusinessId(businessId);
        sysFileInfo.setChildType(childType);
        List<SysFileInfo> sysFileInfos = sysFileInfoMapper.selectSysFileInfoList(sysFileInfo);
        return sysFileInfos;
    }
}

